<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/17 11:28
 * Modified Time: 2019/7/17 11:28
 * Version: 2.0
 */

namespace App\Modules\Store\Repositories;


use App\Models\StoreLike;
use Flower\Core\Repository\Eloquent\BaseRepository;

class StoreLikeRepository extends BaseRepository
{
    public function model()
    {
        return StoreLike::class;
    }

    public function getAmountRate($storeId,$date,$compareDate){
	    return $this->model
		    ->selectRaw('date(created_at) as dates, count(id) as counts')
		    ->whereRaw('(store_id='.$storeId.' and  date(created_at)="'.$date.'")')
		    ->orWhereRaw('(store_id='.$storeId.' and date(created_at)="'.$compareDate.'") group by date(created_at)')
		    ->get()->toArray();
    }

    /**
     * 关注店铺
     * @param $member_id
     * @param $store_id
     * @return mixed
     */
    public function likeStore($member_id,$store_id){
        $user_like = $this->model->where('member_id',$member_id)->where('store_id',$store_id)->first();
        if($user_like){
            return $user_like;
        }
        return $this->create(['member_id'=>$member_id,'store_id'=>$store_id]);
    }

    /**
     * 取关店铺
     * @param $member_id
     * @param $store_id
     * @return bool
     */
    public function unLikeStore($member_id,$store_id){
        $user_like = $this->model->where('member_id',$member_id)->where('store_id',$store_id)->first();
        if(!$user_like){
            return true;
        }
        return $user_like->delete();
    }
}